package com.xxx.mapper;

import com.xxx.eneity.Contest;
import com.xxx.eneity.ContestNotice;
import com.xxx.eneity.ContestSpecies;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ContestMapper {
    @Insert("insert into t_species (name, is_deleted) values (#{speciesName}, 0)")
    public int addContestSpecies(String speciesName);

    @Select("select * from t_species where is_deleted = 0")
    public List<ContestSpecies> getContestSpeciesList();

    @Update("update t_species set is_deleted = 1 where id = #{speciesId}")
    public int removeContestSpecies(Integer speciesId);

    @Insert("insert into t_contest (name, species_id, info, start_date, end_date, is_deleted) values (#{name}, #{speciesId}, #{info}, #{startDate}, #{endDate}, 0)")
    public int addContest(Contest contest);


    @Update("update t_contest set name = #{name}, info = #{info}, start_date = #{startDate}, end_date = #{endDate} where id = #{id}")
    public int editContest(Contest contest);

    @Insert("insert into t_contest_notice (contest_id, title, context, insert_time, update_time) values (#{contestId}, #{title}, #{context}, now(), now())")
    public int addContestNotice(ContestNotice contestNotice);

    @Select("select * from t_contest_notice where contest_id = #{contestId}")
    public List<ContestNotice> getContestNoticeList(Integer contestId);

    @Update("update t_contest_notice set title = #{title} , context = #{context}, update_time = now() where id = #{id}")
    public int editContestNotice(ContestNotice contestNotice);

    @Delete("delete from t_contest_notice where id = #{contestNoticeId}")
    public int deleteContestNotice(Integer contestNoticeId);

    @Select("<script>" +
            "select * from " +
            "((select * from t_contest where is_deleted = 0 and species_id = #{contestSpeciesId} " +
            "<if test='status==0'>" +
            "and end_date &gt; now() " +
            "</if>" +
            "<if test='status==1'>" +
            "and end_date &lt; now() " +
            "</if>" +
            " ) as a " +
            "left join " +
            "(select count(*) as teamNumber, contest_id from t_team where is_deleted = 0 group by contest_id ) as b " +
            "on a.id = b.contest_id )" +
            "</script>")
    public List<Contest> getContestList(Integer contestSpeciesId, Integer status);

    @Update("update t_species set name = #{contestSpeciesName} where id = #{contestSpeciesId}")
    public int editContestSpecies(Integer contestSpeciesId, String contestSpeciesName);

    @Update("update t_contest set is_deleted = 1 where id = #{contestId}")
    public int removeContest(Integer contestId);
}
